
$(document).ready(function(){
    var tgl=$("#tgl_cuti").val();
	
    getCuti(tgl);

    $("#tgl_cuti").keyup(function() {
        var tgl=$("#tgl_cuti").val();;
        getCuti(tgl);
    });
	$("#btn_simpan").click(function(){
		$.getJSON('controller/cont.list_pegawai.php',$("#frm_detail").serialize(),function(data){
			if(data.msg){
				goAdd();
				$("#nip_detail").html(data.cuti.nip);
                $("#nama_detail").html(data.pegawai.nama);
                $("#tgl_detail").html(formattedDate(data.cuti.tglAwal)+" s.d. "+formattedDate(data.cuti.tglAkhir));
                $("#jns_detail").html(data.ref);
			}
			return false;
		});
		return false;
	});
});

function formattedDate(date) {
    var d = new Date(date || Date.now()),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [day, month, year].join('-');
}

function getCuti(tgl){
    $.getJSON('controller/cont.list_pegawai.php',{aksi_get_pegawai:true,tgl_cuti:tgl},function(data){
        $('#list_pegawai tbody').html('');
        var tab = '';
        var counter=0;

        if (data.msg) {
            $.each(data.cuti, function(index, cuti) {
                if(cuti.cutinya!==false){
                    counter++;
                    tab += "<tr>";
                    tab += "<td>" + cuti.pegawai.nip + "</td>";
                    tab += "<td>" + cuti.pegawai.nama + "</td>";
                    tab += "<td> Cuti s.d. "+cuti.cutinya.tglAwal+" </td>";
                    tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + cuti.cutinya.id + "\"/></td>";
                    tab += "</tr>";
                }
            });
            if(counter===0){
                tab += "<tr>"
                tab += "<td colspan=\"5\">Data Tidak Ada</td>";
                tab += "</tr>";
            }
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"5\">Data Tidak Ada</td>";
            tab += "</tr>";
        }
        $('#list_pegawai tbody').append(tab);
    });
}